package setup

import (
	"fmt"
	"strings"
	"tone-agent/core"
	"tone-agent/entity"
)

func InitPath() {
	entity.InitPathConst()
	paths := []string{entity.ResultPath, entity.ScriptPath, entity.LogPath, entity.ConfigPath, entity.TemplatesPath, entity.TaskOutputPath}
	for _, path := range paths {
		if !core.FileExist(path) {
			core.MakeDir(path)
		}
	}

	configYaml := fmt.Sprintf("%s/%s.yaml", entity.ConfigPath, entity.APPName)
	appYaml := fmt.Sprintf("%s/app.yaml", entity.ConfigPath)
	if !core.FileExist(configYaml) {
		core.WriteFile(configYaml, []byte(fmt.Sprintf(entity.ConfigYaml, "")), 0666)
	}
	if !core.FileExist(appYaml) {
		core.WriteFile(appYaml, []byte(entity.APPYaml), 0666)
	}

	htmls := map[string]string{
		fmt.Sprintf("%s/config.html", entity.TemplatesPath): strings.Replace(entity.LoginHTML, "%s", entity.Version, 1),
		fmt.Sprintf("%s/login.html", entity.TemplatesPath):  entity.ConfigHTML,
	}
	for path, content := range htmls {
		if !core.FileExist(path) {
			core.WriteFile(path, []byte(content), 0666)
		}
	}

	checkHeartbeatScript := fmt.Sprintf("%s/.heartbeat-checking.sh", entity.LogPath)
	if !core.FileExist(checkHeartbeatScript) {
		core.WriteFile(checkHeartbeatScript, []byte(entity.CheckHeartbeatScript), 0666)
	}
}
